package com.emode.action;

import java.io.PrintWriter;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;









import org.apache.struts2.ServletActionContext;
import org.json.JSONArray;
import org.json.JSONObject;

import com.emode.base.BaseAction;
import com.emode.entity.Bbsusers;
import com.emode.entity.UserSign;

public class UserSignAction extends BaseAction<UserSign>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 6360476383024595314L;
	private Map<String,Object> dataMap;
	public String tosign(){
		
		return "sign";
	}
	
	
	public String dosign() throws Exception{
		ServletActionContext.getResponse().setContentType(
				"text/javascript;charset=UTF-8");
		PrintWriter out = ServletActionContext.getResponse().getWriter();
		 Bbsusers user = getCurrentUser();
		try {
			// 先查询是否已经签到
			boolean ifHasSigned = signservice.ifHasSigned(user.getUid());
			System.out.println(ifHasSigned);
			if(ifHasSigned) {
				out.print(1);
			} else {
				UserSign sign = new UserSign();
				Date signDate = new Date();
				sign.setSignTime(signDate);
				Calendar cal = Calendar.getInstance();
			    int day = cal.get(Calendar.DATE);
			    int month = cal.get(Calendar.MONTH) + 1;
			    int year = cal.get(Calendar.YEAR);
			    sign.setSignTag(year + "" + month + "" + day);
			    String ip=ServletActionContext.getRequest().getRemoteAddr();
			    sign.setSignIp(ip);
			   
			    sign.setSigner(user);
			    signservice.save(sign);
			    out.print(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	//得到签到list 集合
	public String getSignList() throws Exception{
		ServletActionContext.getResponse().setContentType(
				"text/javascript;charset=UTF-8");
		PrintWriter out = ServletActionContext.getResponse().getWriter();
		System.out.println("month"+model.getMonth()+""+model.getYear());
		JSONArray json = new JSONArray();
		Bbsusers user = getCurrentUser();
		try {
			List<UserSign> signList = signservice.listSign(model.getYear() ,model.getMonth(),user);
			for(UserSign u:signList){
				 JSONObject jo = new  JSONObject();
				 jo.put("sign_time", u.getSignTime());
				 jo.put("signTag", u.getSignTag());
				 json.put(jo);
			}
			System.out.println(json.toString());
			out.print(json.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}


	public Map<String, Object> getDataMap() {
		return dataMap;
	}


	

}
